package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.AdapterJFShangCheng;
import com.lc.afbx.conn.GetJiFenShangCheng;
import com.lc.afbx.conn.PostApiExchanged;
import com.lc.afbx.interf.ExchangeGoodsInterf;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshGridView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/5/23.
 */
public class IntegralExchangeActivity extends BaseActivity implements View.OnClickListener, ExchangeGoodsInterf {


    private PullToRefreshGridView pullToRefreshGridView;
    private GridView integral_exchange_gv;

    private List<GetJiFenShangCheng.Data> lists = new ArrayList<>();;
    private AdapterJFShangCheng adapter;

    //兑换奖品借口
    private PostApiExchanged postApiExchanged = new PostApiExchanged(new AsyCallBack<PostApiExchanged.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiExchanged.Info info) throws Exception {

            UtilToast.show(context, "兑换成功");

            IntegralActivity.RrfreshIntegralCallback rrfreshIntegralCallback = (IntegralActivity.RrfreshIntegralCallback) getAppCallBack(IntegralActivity.class);

            rrfreshIntegralCallback.refreshIntegral();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    //奖品展示借口
    private GetJiFenShangCheng getJiFenShangCheng = new GetJiFenShangCheng(new AsyCallBack<GetJiFenShangCheng.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetJiFenShangCheng.Info info) throws Exception {

            lists.addAll(info.data);

            adapter.notifyDataSetChanged();

            setLastUpdateTime();

        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshGridView.onPullDownRefreshComplete();

            pullToRefreshGridView.onPullUpRefreshComplete();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_exchange);

        pullToRefreshGridView = ((PullToRefreshGridView) findViewById(R.id.integral_exchange_gv));
        pullToRefreshGridView.setScrollLoadEnabled(false);
        pullToRefreshGridView.setPullLoadEnabled(false);
        pullToRefreshGridView.setPullRefreshEnabled(true);

        pullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {

                lists.clear();

                getJiFenShangCheng.execute(IntegralExchangeActivity.this);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {

            }
        });

        integral_exchange_gv = pullToRefreshGridView.getRefreshableView();
        integral_exchange_gv.setNumColumns(2);
        integral_exchange_gv.setVerticalScrollBarEnabled(false);

        integral_exchange_gv.setAdapter(adapter = new AdapterJFShangCheng(context, lists));

        getJiFenShangCheng.execute(this);
    }

    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.back:

                finish();
                return;

            case R.id.integral_exchange_record_rl:

                startActivity(new Intent(this, IntegralExchangeRecordActivity.class));
                break;

        }


    }


    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        pullToRefreshGridView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        SimpleDateFormat mDateFormat = new SimpleDateFormat("HH:mm:ss");
        return mDateFormat.format(new Date(time));
    }


    //回调实现方法
    @Override
    public void exchangeGoodsListener(String goodId) {
        postApiExchanged.id = goodId;
        postApiExchanged.userid = BaseApplication.BasePreferences.readUserId();
        postApiExchanged.execute(context);
    }
}
